package com.youtui.manager.listener;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

public class SystemListener implements ServletContextListener, HttpSessionListener {

	@Override
	public void contextInitialized(ServletContextEvent sce) {
		// 获取的spring上下文
		//ApplicationContext ac = WebApplicationContextUtils.getRequiredWebApplicationContext(sce.getServletContext());
		/*PrivilegeService privilegeService = ac.getBean(PrivilegeService.class);
		List<Privilege> topPrivilege = null;
		try {
			// 所有权限树形结构
			topPrivilege = privilegeService.selectAllTreePrivilege();
		} catch (Exception e) {
			e.printStackTrace();
		}
		sce.getServletContext().setAttribute("topPrivilege", topPrivilege);*/
	}

	@Override
	public void contextDestroyed(ServletContextEvent sce) {

	}

	@Override
	/** session创建 */
	public void sessionCreated(HttpSessionEvent se) {
		ServletContext servletContext = se.getSession().getServletContext();
		Integer currentOnlineNumber = (Integer) servletContext.getAttribute("currentOnlineNumber");
		if (currentOnlineNumber == null) {
			currentOnlineNumber = 1;
		} else {
			currentOnlineNumber = currentOnlineNumber + 1;
		}
		servletContext.setAttribute("currentOnlineNumber", currentOnlineNumber);
	}

	@Override
	/** session销毁 */
	public void sessionDestroyed(HttpSessionEvent se) {
		ServletContext servletContext = se.getSession().getServletContext();
		Integer currentOnlineNumber = (Integer) servletContext.getAttribute("currentOnlineNumber");
		if (currentOnlineNumber != null) {
			currentOnlineNumber = currentOnlineNumber - 1;
			servletContext.setAttribute("currentOnlineNumber", currentOnlineNumber);
		}
	}

}
